package dp.mobile.eden;

import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import dp.mobile.system.SoundManager;

public class TwitterAct extends Activity {
	public static final String CONSUMER_KEY = "JXTrU2zqPSLEtaymzXPA";
	public static final String CONSUMER_SECRET = "W5qu50Yb07xMpY3WJNWceqmzBwnO5Rpt92fp4KXFI";
	private final String CALLBACK_URL = "Eden://TwitterAct";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d("TWIT","masuk");
		setContentView(R.layout.twitter);
		
		mLevel	= this.getIntent().getExtras().getString(GameActivityBase.LEVEL_EXTRAS);
		mScores	= this.getIntent().getExtras().getInt(GameActivityBase.SCORE_EXTRAS);
		
		initComp();
	}
	
	private void initComp(){
		mWebView = (WebView) findViewById(R.id.twitterWebView);
		mWebView.setWebViewClient(new TwitterWebViewClient());
		
		doOAuth();
	}
	
	private void doOAuth(){
		try {
			httpOauthConsumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
			httpOAuthprovider = new DefaultOAuthProvider("http://api.twitter.com/oauth/request_token", "http://api.twitter.com/oauth/access_token", "http://api.twitter.com/oauth/authorize");
			String authURL = httpOAuthprovider.retrieveRequestToken(httpOauthConsumer, CALLBACK_URL);
			
			mWebView.loadUrl(authURL);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		SoundManager.instance(getBaseContext()).temporarySetMute(true);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		SoundManager.instance(getBaseContext()).temporarySetMute(false);
	}
	
	//PROPERTIES
	private int mScores;
	private String mLevel;
	
	//Twitter thingy
	private CommonsHttpOAuthConsumer	httpOauthConsumer;
	private OAuthProvider				httpOAuthprovider;
	
	private Twitter twitter;
	private WebView	mWebView;
	
	//Inner class webViewClient
	private class TwitterWebViewClient extends WebViewClient {
	    @Override
	    public boolean shouldOverrideUrlLoading(WebView view, String url) {
	    	Log.d("overrideURLLoading", "Url = " + url);
	    	
			if(url != null && url.startsWith(CALLBACK_URL)){
				String verifier = Uri.parse(url).getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
				
				try {
					httpOAuthprovider.retrieveAccessToken(httpOauthConsumer, verifier);
					
					// TODO: you might want to store token and token_secret in you app settings!!!!!!!
					AccessToken a = new AccessToken(httpOauthConsumer.getToken(), httpOauthConsumer.getTokenSecret());
					
					//init Twitter4J
					twitter = new TwitterFactory().getInstance();
					twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
					twitter.setOAuthAccessToken(a);
					
					//Create a tweet
					String tweet = "Just completed level " + mLevel + " with score " + mScores + " #DefendersApocalypse";
					
					//Send tweet
					twitter.updateStatus(tweet);
					Log.d("FINISH TWITTER", "SELESAI TWITTERACT = " + tweet);
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				finish();
			} else
				view.loadUrl(url);
	        return true;
	    }
	}
}
